/*
 *  Copyright 2013 HCIWriter
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.hci.writer.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.activity.shared.CachingActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.hci.writer.client.mvp.AppPlaceHistoryMapper;
import com.hci.writer.client.mvp.MainActivityMapper;
import com.hci.writer.client.mvp.NavActivityMapper;
import com.hci.writer.client.place.GreetingPlace;

public class HCIWriter extends Composite implements EntryPoint {
	
	private Place defaultPlace = new GreetingPlace("");
	
	@UiField SimplePanel navContainer;
	@UiField SimplePanel mainContainer;

	private static HCIWriterUiBinder uiBinder = GWT
			.create(HCIWriterUiBinder.class);

	interface HCIWriterUiBinder extends UiBinder<Widget, HCIWriter> {
	}

	public HCIWriter() {
		initWidget(uiBinder.createAndBindUi(this));
		RootPanel.get("content").add(this);
	}

	@Override
	public void onModuleLoad() {
		ClientFactory clientFactory = GWT.create(ClientFactory.class);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();
		
		// Start ActivityManager for the navigation widget with our ActivityMapper
		ActivityMapper navActivityMapper = new NavActivityMapper(clientFactory);
		CachingActivityMapper navCachingActivityMapper = new CachingActivityMapper(navActivityMapper);
		ActivityManager navActivityManager = new ActivityManager(navCachingActivityMapper, eventBus);
		navActivityManager.setDisplay(navContainer);

		// Start ActivityManager for the main widget with our ActivityMapper
		ActivityMapper mainActivityMapper = new MainActivityMapper(clientFactory);
		CachingActivityMapper mainCachingActivityMapper = new CachingActivityMapper(mainActivityMapper);
		ActivityManager mainActivityManager = new ActivityManager(mainCachingActivityMapper, eventBus);
		mainActivityManager.setDisplay(mainContainer);
		
		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);

		// Go to the place represented on URL else default place
//		historyHandler.handleCurrentHistory();
		
		placeController.goTo(new GreetingPlace(""));
	}

}
